<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="../js/jquery-3.5.1.min.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			.d1 {
				width: 300px;
				height: 150px;
				border: 1px solid #CCCCCC;
				background-color: #000000;
				float: left;
			}

			input {
				width: 50px;
				height: 30px;
				text-align: center;
				border: 4px solid #CCCCCC;
				margin-left: 15px;
				margin-top: 20px;
			}

			.b1,
			.b2 {
				background-color: #08BA61;
				border: 2px solid red;
				border-radius: 10px 10px 10px 10px;
				width: 240px;
				height: 30px;
				align-content: center;
				padding-bottom: 45px;
				font-size: 30px;
				color: white;
				margin-left: 30px;
				margin-top: 20px;
			}

			.b2 {
				display: none;
			}

			span {
				color: white;
				font-size: 30px;
				margin-left: 22.5px;
			}

			.d2 {
				display: none;
				background-color: red;
				color: white;
				width: 300px;
			}

			.d3 {
				width: 300px;
				height: 30px;
				float: left;
				border: 1px solid red;
				background-color: yellow;
				margin-top: 20px;
				display: none;
			}
		</style>
	</head>
	<body>
		<div class="d1">
			<input type="number" class="p1" value="1" /><span>分钟</span>
			<input type="number" class="p2" value="59" /><span>秒</span><br>
			<button class="b1" type="button">启动</button>
			<button class="b2" type="button">停止</button>
		</div>
		<div class="d3">请输入合法字符（分：0-60；秒：0-60）</div>

		<div class="d2">请输入有效字符哦!<br>（分：0-60；秒：0-60）</div>
		<script type="text/javascript">
			$(function() {
				// 字符合法性判断
				$(".p1,.p2").hover(
					function() {
						$(".d2").show();
						$(".d3").hide();
					},
					function() {
						var p1 = $(".p1").val();
						var p2 = $(".p2").val();
						if (p1 > 60 || p1 < 0 || p2 < 0 || p2 > 60) {
							$(".d3").show();
							$(".p1").val(0);
							$(".p2").val(0);
						}
						$(".d2").hide();
					}
				);
				
				// 开始计时
				var myfun = $(".b1").click(function() {
					$(this).hide(); //开始按钮隐藏
					$(".b2").show(); //停止按钮显示
					var s = setInterval(function(){start()}, 1000);
					function start() {
						var a = $(".p1").val();
						var b = $(".p2").val(); //(2-3正则表达式)
						var sum1 = b;
						var sum2 = a;
						if (sum1 == 0 && sum2 > 0) {
							sum1 = 60;
							sum2 = sum2 - 1;
						}
						sum1--;
						$(".p2").val(sum1);
						$(".p1").val(sum2);
						// 时间结束自动停止
						if (sum1 == 0 && sum2 == 0) {
							clearInterval(s);
							alert("计时结束！")
						}
					}
					// 提前手动停止
					$(".b2").click(function() {
						$(this).hide();
						$(".b1").show();
						clearInterval(s);
					})
				})
			})
		</script>
	</body>
</html>
